package com.firewof.redis;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.Before;
import org.junit.Test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

public class RedisDriver {
	
	private Jedis jedis = null;
	
	@Before
	public void init(){
		jedis = new Jedis("127.0.0.1", 6379);
	}
	
	/**
	 * 连接Redis服务，
	 * 1.如果连接成功，则jedis.ping()返回PONG,
	 * 2.连接失败会抛出JedisConnectionException异常
	 */
	@Test
	public void testConnection(){
		System.out.println(jedis.ping());
	}
	
	/**
	 * 对key的一些操作
	 */
	@Test
	public void testKey(){
		//查看所有key值，等价于：keys *
		Set<String> keys = jedis.keys("*");
		Iterator<String> iterator = keys.iterator();
		while (iterator.hasNext()) {
			System.out.println(iterator.next());
		}
		
		//判断某个key是否存在
		System.out.println(jedis.exists("key1"));
		
		//查看某个key的剩余存活时间
		System.out.println(jedis.ttl("key1"));
	}
	
	/**
	 * 对String类型数据的操作
	 */
	@Test
	public void testString(){
		//设置key，等同于set key v
		jedis.set("kStr1", "first String Key");
		
		//一次性设置多个
		jedis.mset("kstr2","vstr2","kstr3","vstr3");
		
		//一次性获取多个值
		System.out.println(jedis.mget("key1","key3","key5"));
	}
	
	/**
	 * 对List数据类型的操作
	 */
	@Test
	public void testList(){
		//存放list
		jedis.lpush("listkey", "张三","李四","王五");
		
		//获取list
		List<String> list = jedis.lrange("listkey", 0, -1);
		System.out.println(list);
	}
	
	/**
	 * 对set数据类型操作
	 */
	@Test
	public void testSet(){
		//添加集合元素
		jedis.sadd( "orders" , "jd001" ); 
		jedis.sadd( "orders" , "jd002" ); 
		jedis.sadd( "orders" , "jd003" ); 
		jedis.sadd( "orders" , "jd004" ); 
		
		//获取集合中的元素
		Set<String> set = jedis.smembers("orders");
		Iterator<String> iterator = set.iterator();
		while (iterator.hasNext()) {
			System.out.println(iterator.next());
		}
	}
	
	/**
	 * 对hash类型数据的操作
	 */
	@Test
	public void testHash(){
		//存放hash
		jedis.hset("user1", "name", "liuxing");
		jedis.hset("user1", "age", "27");
		jedis.hset("user1", "content", "cool!");
		
		//获取user1的所有属性和值
		Map<String, String> map = jedis.hgetAll("user1");
		Set<String> set = map.keySet();
		Iterator<String> iterator = set.iterator();
		while (iterator.hasNext()) {
			String key = iterator.next();
			System.out.println(key + ": "+map.get(key));
		}
	}
	
	/**
	 * 对有序集合的操作
	 */
	@Test
	public void testZSort(){
		//往有序集合添加数据
		jedis.zadd("zhangsan", 60, "Chinese");
		jedis.zadd("zhangsan", 100, "English");
		jedis.zadd("zhangsan", 80, "Math");
		jedis.zadd("zhangsan", 50, "Java");
		
		//获取操作
		Set<String> zrange = jedis.zrange("zhangsan", 0, -1);
		Iterator<String> iterator = zrange.iterator();
		while (iterator.hasNext()) {
			System.out.println(iterator.next());
		}
	}
	
	/**
	 * 事务
	 */
	@Test
	public void testTX(){
		jedis.watch("balance");//注意要加上这一句，不然的话总是会提交成功
		Transaction tx = jedis.multi();
		try {
			//这里线程暂停，如果去客户端修改了balance，那么这里的修改操作会不成功
			Thread.sleep(5000); 
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		tx.decrBy("balance", 20);
		List<Object> list = tx.exec();
		System.out.println("xxx");
	}
}
